﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QSHC.QSchedule.Silverlight.Helpers;
using System.Linq;

namespace QSHC.QSchedule.Silverlight.Authorizations
{
    public class QScheduleAutherization
    {
        public static bool ControlsIsEnabled(string parameter)
        {
            var allEventElement = EmployeeHelper.QScheduleAutherization;
            var controlEventElement = allEventElement.Where(p => p.Attribute("Name").Value == parameter).FirstOrDefault();

            if (controlEventElement != null)
            {
                if (controlEventElement.Elements().Count() > 0)
                {
                    foreach (var a in controlEventElement.Elements())
                    {
                        switch (a.Name.LocalName)
                        {
                            case "Role":
                                if (WebContext.Current.User.IsInRole(a.Attribute("Name").Value)
                                    && a.Attribute("IsEnabled").Value == "True")
                                    return true;
                                break;
                            case "User":
                                if (EmployeeHelper.LoggedEmployee == a.Attribute("Name").Value
                                    && a.Attribute("IsEnabled").Value == "True")
                                    return true;
                                break;
                            default:
                                return false;
                                break;
                        }
                    }
                }
                else
                    return true;
            }
            return false;
        }

        public static Visibility ControlsVisibility(string parameter)
        {
            if (WebContext.Current.User.IsInRole("Administrator"))
                return Visibility.Visible;

            var allEventElement = EmployeeHelper.QScheduleAutherization;
            var controlEventElement = allEventElement.Where(p => p.Attribute("Name").Value == parameter).FirstOrDefault();

            if (controlEventElement != null)
            {
                if (controlEventElement.Elements().Count() > 0)
                {
                    foreach (var a in controlEventElement.Elements())
                    {
                        switch (a.Name.LocalName)
                        {
                            case "Role":
                                if (WebContext.Current.User.IsInRole(a.Attribute("Name").Value)
                                    && a.Attribute("Visibility").Value == "Visible")
                                    return Visibility.Visible;
                                break;
                            case "User":
                                if (EmployeeHelper.LoggedEmployee == a.Attribute("Name").Value
                                    && a.Attribute("Visibility").Value == "Visible")
                                    return Visibility.Visible;
                                break;
                            default:
                                return Visibility.Collapsed;
                                break;
                        }
                    }
                }
                else
                    return Visibility.Visible;
            }

            return Visibility.Collapsed;
        }
    }
}
